package com.film.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.film.dao.FilmDao;
import com.film.dao.FilmOfficeDao;
import com.film.dao.ShowingsDao;
import com.film.dao.impl.FilmDaoImpl;
import com.film.dao.impl.FilmOfficeDaoImpl;
import com.film.dao.impl.ShowingsDaoImpl;
import com.film.domain.Film;
import com.film.domain.FilmOffice;
import com.film.domain.FilmShowings;
import com.film.service.ShowingsService;

public class ShowingsServiceImpl implements ShowingsService {
	
	private ShowingsDao showingsDao = new ShowingsDaoImpl();
	private FilmDao filmDao = new FilmDaoImpl();
	private FilmOfficeDao filmOfficeDao = new FilmOfficeDaoImpl();

	@Override
	public List<FilmShowings> selectAllShowings(int showingsStatusCode) {
		return showingsDao.selectAllShowings(showingsStatusCode);
	}

	@Override
	public List<FilmShowings> selectShowingsByFilmId(int filmId, int showingsStatusCode) {
		return showingsDao.selectShowingsByFilmId(filmId, showingsStatusCode);
	}

	@Override
	public List<FilmShowings> selectShowingsByDate(String date, int filmId, int showingsStatusCode) {
		return showingsDao.selectShowingsByDate(date, filmId, showingsStatusCode);
	}

	@Override
	public List<String> selectShowingsDateByFilm(int filmId, int showingsStatusCode) {
		return showingsDao.selectShowingsDateByFilm(filmId, showingsStatusCode);
	}

	@Override
	public FilmShowings seleShowingsById(int showingsId, int showingsStatusCode) {
		return showingsDao.selectShowingsById(showingsId, showingsStatusCode).get(0);
	}

	@Override
	public List<String> selectShowingsAll() {
		
		return showingsDao.selectShowingsAll();
	}

	@Override
	public int deleteFilmShowings(int showingsId) {
		// TODO Auto-generated method stub
		return showingsDao.deleteFilmShowings(showingsId);
	}
	
	
	
	@Override
	public int addFilmShowings(int filmId, String showingsDay, String startTime, int officeId, Double price) {
		System.out.println("进入了方法");
		//首先判断场次排期日期是不是在电影上映时间之后
		if(checkFilmShowDay(filmId, showingsDay)) {
			//查询当前放映厅当前日期的所有的排期信息
			List<FilmShowings> list = showingsDao.selectFilmShowingsByOfficAndDate(officeId, showingsDay);
			boolean flag = false;
			showingsDay = showingsDay.replace("-", "/");
			//如果当前放映开始时间与上述信息重复或在上述信息开始~结束时间区间内则增加失败
			for (int i = 0; i < list.size(); i++) {
				FilmShowings showings = list.get(i);
				if(i == list.size() - 1) {
					//只有一场电影
					if(list.size() == 1) {
						Date date = new Date(showingsDay+" "+startTime);
						Date showTime = new Date(showingsDay+" "+showings.getShowingsStartTime());
						//待添加电影的场次播放时间
						long t1 = date.getTime();
						//原有电影的场次播放时间
						long t2 = showTime.getTime();
						if(t1 < t2) {
							//在原有的电影之前的
							//获取待添加电影的播放时长，计算出结束时间，结束时间必须小于下一场电影的开始时间-20分钟
							long filmTime = 96 * 60 * 1000;
							long emptyTime = 20 * 60 * 1000;
							long endTime = t1 + emptyTime + filmTime;
							if(endTime <= t2) {
								System.out.println("只有一场电影，排在之前，能放下");
								flag = true;
								break;
							}else {
								System.out.println("只有一场电影，排在之前，放不下");
							}
						}else if(t1 > t2) {
							//获取原有电影的结束时间
							long endTime = new Date(showingsDay+" "+showings.getShowingsEndTime()).getTime();
							long emptyTime = 20 * 60 * 1000;
							if(endTime + emptyTime <= t1) {
								System.out.println("一场电影，在排在之后，能放下");
								flag = true;
								break;
							}else {
								System.out.println("一场电影，在排在之后，放不下");
							}
						}else {
							System.out.println("只有一场电影，时间冲突，放不下");
						}
					}else {
						//不止一场电影，但这是最后一场
						//获取最后一场电影的结束时间
						long endTime = new Date(showingsDay+" "+showings.getShowingsEndTime()).getTime();
						long emptyTime = 20 * 60 * 1000;
						//添加的电影结束时间最晚不能超过当天
						long dayEndTime = new Date(showingsDay).getTime() + (24 * 60 * 60 * 1000);
						System.out.println("最后一场："+endTime);
						System.out.println(new Date(showingsDay+" "+list.get(list.size()-1).getShowingsEndTime()).getTime());
						//待添加电影的播放时间
						Date showTime = new Date(showingsDay+" "+startTime);
						System.out.println(showTime.getTime());
						long end = showTime.getTime() + 96 * 60 * 1000;
						if(endTime + emptyTime <= showTime.getTime() && end < dayEndTime) {
							System.out.println("安排在最后一场电影，能放下");
							flag = true;
							break;
						}else {
							System.out.println("安排在最后一场，不能放下，按道理不会出现");
						}
					}
				}else {
					System.out.println(showingsDay);
					String end = showings.getShowingsEndTime();
					Date date = new Date(showingsDay+" "+end);
					FilmShowings showings1 = list.get(i+1);
					String start = showings1.getShowingsStartTime();
					Date date1 = new Date(showingsDay+" "+start);
					Date startDate = new Date(showingsDay+" "+startTime);
					long filmTime = 125 * 60 * 1000;
					if(startDate.getTime() > date.getTime() && startDate.getTime() < date1.getTime()) {
						System.out.println(showings);
						System.out.println(showings1);
						System.out.println("1234156");
						long t1 = new Date(showingsDay+" "+showings1.getShowingsStartTime()).getTime()+(20*60*1000);
						long t2 = new Date(showingsDay+" "+showings.getShowingsEndTime()).getTime();
						System.out.println(t1);
						System.out.println(t2);
						System.out.println(t1 - t2);
						System.out.println(filmTime);
						if(t1 - t2 > filmTime) {
							System.out.println("放在中间能放下");
							flag = true;
							break;
						}else {
							System.out.println("放在中间放不下");
						}
					}
				}
			}
			//如果当前放映厅没有场次安排，则直接添加成功
			if(list.size() == 0) {
				flag = true;
			}
			if(flag) {
				//可以添加
				FilmShowings showings = new FilmShowings();
				Film film = filmDao.selectOneFilm(filmId, 0);
				showings.setFilm(film);
				showings.setShowingsDay(showingsDay);
				showings.setShowingsStartTime(startTime);
				showings.setShowingsEndTime(getEndTime(showingsDay+" "+startTime, filmId));
				FilmOffice office = filmOfficeDao.selectOffice(officeId).get(0);
				showings.setFilmOffice(office);
				showings.setShowingsPrice(price);
				showings.setIsDelete(0);
				return showingsDao.addShowings(showings);//添加成功
			}
			return 0;//场次安排时间冲突了
		}
		return -1; //场次安排时间早于电影上映时间
	}
	
	/**
	 * 根据电影开始时间获取电影结束时间
	 * @param startTime 开始时间
	 * @param filmId 电影id
	 * @return
	 * 		返回电影结束时间
	 */
	private String getEndTime(String startTime,int filmId) {
		Date t1 = new Date(startTime);
		Film film = filmDao.selectOneFilm(filmId, 0);
		String filmTime = film.getFilmTime().substring(0,film.getFilmTime().indexOf('分'));
		System.out.println(filmTime);
		long end = t1.getTime() + (Integer.parseInt(filmTime) * 60 * 1000);
		Date endTime = new Date(end);
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		return dateFormat.format(endTime);
	}
	
	/**
	 * 判断场次安排的时间是不是在电影上映之后
	 * @param filmId 电影id
	 * @param showingsDay 场次播放时间
	 * @return
	 * 		是在之后 return true;
	 * 		不是之后 return flase;
	 */
	private boolean checkFilmShowDay(int filmId,String showingsDay) {
		Film film = filmDao.selectOneFilm(filmId, 0);
		String filmShowDay = "";
		if(film != null) {
			filmShowDay = film.getFilmShowTime();
		}
		int index = filmShowDay.indexOf('中');
		filmShowDay = filmShowDay.substring(0,index);
		filmShowDay = filmShowDay.replace("-", "/");
		showingsDay = showingsDay.replace("-", "/");
		long filmDay = new Date(filmShowDay).getTime();
		long showingsDay1 = new Date(showingsDay).getTime();
		return filmDay <= showingsDay1;
	}
	

	@Override
	public List<FilmShowings> limitShowings(int pageNum) {
		int stripNum = 10;//默认分页的查询条数为5条
		
		int pageNums = (pageNum-1)*stripNum;
		return showingsDao.limitShowings(pageNums, stripNum);
	}

}
